#include <bits/stdc++.h>

using namespace std;

void degree_generator (const char *offset_filename, const char *edge_info_filename, const char *result_file) {
    FILE *offset_fp, *info_fp, *degree_fp;
    int *edge_list;
    float *vertex_degree;
    int vertex_num, edge_num;

    if ((offset_fp = fopen (offset_filename, "r")) == NULL) {
        cout << "[ERROR] Failed to open file " << offset_filename << endl;
        exit (-1);
    }
    cout << "[INFO] Open file " << offset_filename << endl;
    if ((info_fp = fopen (edge_info_filename, "r")) == NULL) {
        cout << "[ERROR] Failed to open file " << edge_info_filename << endl;
        exit (-1);
    }
    cout << "[INFO] Open file " << edge_info_filename << endl;
    if ((degree_fp = fopen (result_file, "w")) == NULL) {
        cout << "[ERROR] Failed to open file " << result_file << endl;
        exit (-1);
    }
    cout << "[INFO] Open file " << result_file << endl;

    fscanf (offset_fp, "%d", &vertex_num);
    fscanf (info_fp, "%d", &edge_num);

    cout << "[INFO] Vertex_num = " << vertex_num << ", Edge_num = " << edge_num << endl;

    edge_list = (int *) calloc (edge_num, sizeof(int));
    vertex_degree = (float *) calloc (vertex_num, sizeof(float));

    for (int i = 0; i < vertex_num; i ++) {
        vertex_degree[i] = 0.0;
    }

    for (int i = 0; i < edge_num; i ++) {
        fscanf (info_fp, "%d", &edge_list[i]);
        vertex_degree[edge_list[i]] += 1.0;
    }

    // 根据 info list 生成 degree list
    for (int i = 0; i < edge_num; i ++) {
        // 转化为整数写入
        int *a = (int *)(&vertex_degree[edge_list[i]]);
        fprintf (degree_fp, "%d\n", *a);
    }

    fclose (offset_fp);
    fclose (info_fp);
    fclose (degree_fp);

}

int main(int argc, char **argv) {

    if (argc != 4) {
        cout << "Usage: Parameter (offset_file, info_file, result_file)." << endl;
        exit(-1);
    }

    degree_generator (argv[1], argv[2], argv[3]);

    return 0;
}